package dev.polls.skycube;


import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class GroupActivity extends ListActivity {
	int messageId;
	GetJSon gj = new GetJSon();
	String[] groups = new String[200];
	int countgroups = 0;
	 GroupList gl = new GroupList();
	private int groupid = 1;
	static String attr1;
	static String attr2;
	int stoprepeat = 0;
	

	TextView txtParsedValue;
	TextView txtParsedValue2;

		
	Bitmap bitmap;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.group);	

        
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        
        
        try {

				 SharedPreferences settings = getSharedPreferences("account_info",0);
		 String username = settings.getString("username", "");
		 String password = settings.getString("password", "");
		 
	 JSONArray ja = gj.getMessageList(username, password, groupid);
	 for (int i = 0; i < ja.length(); ++i) 
	 {
			HashMap<String, String> map = new HashMap<String, String>();
		 JSONObject rec = ja.getJSONObject(i);
		 map.put("id",  String.valueOf(i));
	      attr1 = rec.getString("Title");
	    	 attr2 = rec.getString("Body");
		 map.put("name", "" + attr1);
		 map.put("Description", "" + attr2);
     	mylist.add(map);
	    	System.out.println("fgrthj"+ attr1);
	    	System.out.println();

	     
	 }
			
		} catch(JSONException e)        {
       	 Log.e("log_tag", "Error parsing data "+e.toString());
       } catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        
		final Button knop = (Button)findViewById(R.id.getGroups);
		knop.setOnClickListener(new View.OnClickListener() //called when login button is touched
	 	{
	 		public void onClick(View arg0) 
	 		{
	 			Intent i = new Intent(GroupActivity.this, SelectGroups.class);
				startActivity(i);	 		
	 		}
	 	});
        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.messages, 
                new String[] { "name", "Description" }, 
                new int[] { R.id.item_title, R.id.item_subtitle });

setListAdapter(adapter);

final ListView lv = getListView();
lv.setTextFilterEnabled(true);		
	}
	
	
    public void parseJSON() throws ClientProtocolException, URISyntaxException, IOException, JSONException
    {
    			 SharedPreferences settings = getSharedPreferences("account_info",0);
			 String username = settings.getString("username", "");
			 String password = settings.getString("password", "");

    	
		 JSONArray ja = gj.getGroupList(username, password);
		 for (int i = 0; i < ja.length(); ++i) 
		 {
			 JSONObject rec = ja.getJSONObject(i);
		      attr1 = rec.getString("Name");
		    	 attr2 = rec.getString("Description");
		    	txtParsedValue.setText(attr1);
		    	txtParsedValue2.setText(attr2);
		    	System.out.println("fgrthj"+ attr1);
		    	
		    	
			     if(groups[i]!= null) 
			     {
			    	countgroups++;
			    }
		     
		 }
    }

	
		 
			
	/*  public void onViewClick(View v)
	  {
		  
		  TabHandler ParentActivity;
		  switch(v.getId()){
		  
		  case R.id.linearLayout1: messageId = 0; break;

		  case R.id.linearLayout2: messageId = 1; break;
		  case R.id.linearLayout3: messageId = 2; break;

		  case R.id.linearLayout4:messageId = 3; break;
		  case R.id.linearLayout5: messageId = 4; break;
		  }
		  System.out.println(messageId);
		  FileInfo file = new FileInfo();
		  file.setMessageId(messageId);
		  
		  FileActivity.setFile(file);
          ParentActivity = (TabHandler) this.getParent();
          ParentActivity.switchTab(2);
      }*/
    
    
	  /*
	   * 
	   * 
	   */
	  public void grouplist() throws ClientProtocolException, URISyntaxException, IOException, JSONException
		 {
			 SharedPreferences settings = getSharedPreferences("account_info",0);
			 String username = settings.getString("username", "");
			 String password = settings.getString("password", "");
 
			 JSONArray ja = gj.getGroupList(username, password);
			 for (int i = 0; i < ja.length(); ++i) 
			 {
				 JSONObject rec = ja.getJSONObject(i);
			     groups[i] = rec.getString("Name");
			     if(groups[i]!= null) 
			     {
			    	countgroups++;
			    }
			     
			 }
			 System.out.println(countgroups);
			gl.setGroups(countgroups);

		 }
	  @Override
	  public void onResume()
	  {
	     
	     super.onResume();
	     System.out.println(getGroupId());
	  }

	  public void setGroupId(int i)
	  {
		  groupid = i;
	  }
	  public int getGroupId()
	  {
		  return groupid;
	  }
}
	 
	  

